using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Specialized;

namespace WineCellar.Web
{
    public static class UrlHelper
    {

        public static OrderedDictionary ParseUrl(string url)
        {
            const string UrlParseExpression=@"(?<protocol>\w+)://((?<user>[\w]+):?(?<password>\w+)?[@])?(?<hostname>\w+)(:?(?<portNumber>\d+))?(?<appPath>[/\w+.]*)?(\?(?<queryString>[\w+.&=%]*))?";

            Regex regex = new Regex(UrlParseExpression);
            Match match = regex.Match(url);

            OrderedDictionary resultDictionary = new OrderedDictionary();
            string[] groupNames = regex.GetGroupNames();
            foreach (string groupName in groupNames)
            {
                int temp;
                if (!int.TryParse(groupName, out temp))
                    resultDictionary.Add(groupName, match.Groups[groupName].Value);
            }

            return resultDictionary;
        }
    }
}
